<template>
    <view class="goods-item">
        <!-- 左侧侧的盒子 -->
        <view class="goods-item-left">
            <radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioChangeHandler"></radio>
            <image :src="currentImage" class="goods-pic" @error="handleImageError"></image>
        </view>
        <!-- 右侧的盒子 -->
        <view class="goods-item-right">
            <!-- 商品的名字 -->
            <view class="goods-name">{{goods.goods_name}}</view>
            <view class="goods-info-box">
                <view class="goods-price">￥{{formatPrice(goods.goods_price)}}</view>
                <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum"
                    @change="numChangeHandler"></uni-number-box>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            goods: {
                type: Object,
                default: {}
            },
            showRadio: {
                type: Boolean,
                default: false
            },
            showNum: {
                type: Boolean,
                default: false
            }
        },
        name: "my-goods",
        data() {
            return {
                defaultPic: '/static/logo.png', // 使用本地默认图片，避免依赖外部服务
                imageError: false
            };
        },
        computed: {
            currentImage() {
                // 如果图片已出错，则直接使用默认图片
                if (this.imageError) {
                    return this.defaultPic;
                }
                // 否则尝试使用商品图片或默认图片
                return this.convertToHttps(this.goods.goods_small_logo) || this.defaultPic;
            }
        },
        methods: {
            convertToHttps(url) {
                // 全面检查URL有效性
                if (!url || typeof url !== 'string' || url.trim() === '') {
                    return '';
                }
                
                // 转换HTTP为HTTPS
                if (url.startsWith('http://')) {
                    return url.replace('http://', 'https://');
                }
                
                return url;
            },
            // 使用方法而不是过滤器来格式化价格，兼容小程序环境
            formatPrice(num) {
                return Number(num).toFixed(2);
            },
            // 处理图片加载错误
            handleImageError() {
                this.imageError = true;
            },
            // 复选框的点击事件
            radioChangeHandler() {
                this.$emit('radio-change', {
                    goods_id: this.goods.goods_id,
                    goods_state: !this.goods.goods_state,
                })
            },
            // 监听到了的NumberBox的变化
            numChangeHandler(val) {
                this.$emit('num-change', {
                    goods_id: this.goods.goods_id,
                    goods_count: val - 0
                })
                // console.log(val);
            }
        }
    }
</script>

<style lang="scss">
    .goods-item {
        display: flex;
        padding: 10px 5px;
        border-bottom: 1px solid #efefef;

        .goods-item-left {
            margin-left: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .goods-pic {
                width: 100px;
                height: 100px;
                display: block;
            }
        }

        .goods-item-right {
            display: flex;
            flex: 1;
            flex-direction: column;
            justify-content: space-between;

            .goods-name {
                font-size: 13px
            }

            .goods-info-box {
                display: flex;
                justify-content: space-between;
                align-items: center;

                .goods-price {
                    color: #c00000;
                    font-size: 16px
                }
            }
        }
    }
</style>